angular.module('tab-home', ['pop-up','slide-banner','share-model'])

.controller 'TabHomeCtrl', ($location,$scope, $state, $http, $ionicHistory,$ionicViewSwitcher,popUpSrv, $log,$ionicPopup,userData,$ionicSlideBoxDelegate,shareModel,showToast) ->
    logger = $log.getInstance 'TabHomeCtrl'
    logger.info 'load TabHomeCtrl'
    shareModel.set [
        {img:'img/timeline.png',text:'分享到朋友圈',func: -> $scope.goShare0()}
        {img:'img/wechat.png',text:'分享给朋友',func: -> $scope.goShare1()}
    ]
    $scope.goShare0 = ->
        $http.get('/seller/',params:{action:'shareshop',userid:userData.getToken()})
        .success (resp)->
            if resp?.status == '0'
                $scope.shareData = resp
                Wechat.share({
                            message: {
                                title: $scope.shareData.title
                                description: $scope.shareData.description
                                thumb: $scope.shareData.shoplogo
                                media: {
                                    type: Wechat.Type.LINK,
                                    webpageUrl: $scope.shareData.url
                                }
                            },
                            scene: Wechat.Scene.TIMELINE
                        },
                    ()->console.log('分享成功~'),
                    (reason) ->console.log(reason)
                )
            else
                $scope.errorMsg = resp.msg
        .error ->
            showToast '服务器处理失败'

    $scope.goShare1 = ->
        $http.get('/seller/',params:{action:'shareshop',userid:userData.getToken()})
        .success (resp)->
            if resp?.status == '0'
                $scope.shareData = resp
                Wechat.share({
                            message: {
                                title: $scope.shareData.title
                                description: $scope.shareData.description
                                thumb: $scope.shareData.shoplogo
                                media: {
                                    type: Wechat.Type.LINK,
                                    webpageUrl: $scope.shareData.url
                                }
                            },
                            scene: Wechat.Scene.SESSION
                        },
                    ()->console.log('分享成功~'),
                    (reason) ->console.log(reason)
                )
            else
                $scope.errorMsg = resp.msg
        .error ->
            showToast '服务器处理失败'


    $http.get('/advertise/',params:{action:'usercenter'})
    .success (resp) ->
        if resp?.status == '0'
            $scope.banners = resp.body
            $ionicSlideBoxDelegate.$getByHandle('banner').update()
            $ionicSlideBoxDelegate.$getByHandle('banner').loop(true)
        else
            showToast resp.error
    .error ->
        showToast '服务器处理失败'

    $scope.mallName = '我的商城'
    _.extend $scope,
        goGoodsList:  ->
            $ionicViewSwitcher.nextDirection('forward')
            $state.go 'tab.goods-list'
        goTabMarket:  ->
            $ionicViewSwitcher.nextDirection('forward')
            $state.go 'tab.market-home'
        goMallManage:  ->
            $ionicViewSwitcher.nextDirection('forward')
            $state.go 'tab.shop-manage-home'
        goVipList:  ->
            $ionicViewSwitcher.nextDirection('forward')
            $state.go 'tab.vip-list'
        goTabMessage:  ->
            $ionicViewSwitcher.nextDirection('forward')
            $state.go 'tab.message-list-home'
        goWarehouse: ->
            $ionicViewSwitcher.nextDirection('forward')
            $state.go 'tab.warehouse-list'
        goodsAdd: ->
            popUpSrv.do [{img:'img/home/get-from-market.png',desc:'优品库', func:$scope.goYPK},{img:'img/home/self-good-add.png',desc:'自营商品',func:$scope.goGoodsAdd}]
        goYPK: ->
            $state.go 'tab.market-home'
        goGoodsAdd: ->
            $state.go 'tab.goods-add'
        goShare: (e) ->
            shareModel.show e
        goGather:->
            $state.go 'tab.gather-manage'

    $scope.getSellerInfo = ->
        $http.get('/seller/',params:{action:'sellerinfo',userid:userData.getToken()})
        .success (resp) ->
            if resp?.status == '0'
                $scope.mallData = resp
            else
                showToast resp.error
        .error ->
            showToast '服务器处理失败'

    $scope.$on '$ionicView.enter',->
        $scope.getSellerInfo()

